<template>
  <el-container class="home_container">
    <!-- 头部区域 -->
    <el-header>
      <div class="logo">
        <div class="wrap"><img src="@/assets/logo.png"></div>
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="doLogout">退出</el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
         <div class="toggle-button" @click="toggleCollapse">|||</div>
         <ul class="infinite-list" style="overflow: auto">
         <el-menu 
          background-color="#333744" 
          text-color="#fff" 
          active-text-color="#409eff"
          :unique-opened="true" 
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activepath">
          <!-- 一级菜单 -->
          <el-sub-menu :index="`${index + 1}`" v-for="(item, index) in menulist" :key="item.id">
            <!-- 一级菜单模板区域 -->
            <template #title>
              <!-- 图标 -->
              <el-icon :class="item.icon"></el-icon>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/home/'+subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/home/'+subItem.path)">
              <!-- 二级菜单模板区域 -->
              <template #title>
                <!-- 图标 -->
                <el-icon><Menu /></el-icon>
                <!-- 文本 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-sub-menu>
        </el-menu>
        </ul>   
      </el-aside>
      <!-- 右侧内容区域 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup lang="ts">
import useUserStore from '@/stores/modules/user'
import pinia from '@/stores'
import { reactive, ref } from 'vue';
import { useRouter} from 'vue-router';
const userStore = useUserStore(pinia)
  //获取路由器
let $router = useRouter();
console.log(window.sessionStorage.getItem('activepath'))
let isCollapse = ref(false)
let activepath = ref(window.sessionStorage.getItem('activepath'))
const menulist = reactive([{
    "id": 1,
    "authName": "用户管理",
    "path": null,
    "icon": "iconfont icon-user",
    "children": [
        {
            "id": 2,
            "authName": "用户列表",
            "path": "users-list",
            "icon": "iconfont icon-shangpin",
            "children": []
        }, {
            "id": 3,
            "authName": "新增用户",
            "path": "users-add",
            "icon": "iconfont icon-shangpin",
            "children": []
        }, {
            "id": 3,
            "authName": "新增用户",
            "path": "users-edit",
            "icon": "iconfont icon-shangpin",
            "children": []
        }
    ]
},{
    "id": 5,
    "authName": "商品管理",
    "icon": "iconfont icon-shangpin",
    "path": null,
    "children": [
        {
            "id": 6,
            "authName": "商品列表",
            "icon": "iconfont icon-shangpin",
            "path": null,
            "children": []
        }, {
            "id": 7,
            "authName": "商品新增",
            "icon": "iconfont icon-shangpin",
            "path": null,
            "children": []
        }
    ]
}])

const doLogout = async () => {
  await userStore.userLogout();
  $router.push('/login')
}

const toggleCollapse = async () => {
  isCollapse.value = !isCollapse.value
}

// 点击二级菜单保存 path 值
const saveNavState = (clickActivepath) => {
  window.sessionStorage.setItem('activepath', clickActivepath)
  activepath.value = clickActivepath
}
</script>

<style scoped>
.el-menu {
  height: 100vh;
}
.el-header {
  background-color: #373D41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 20px;
  padding-left: 0px;
  color: #fff;
}
.el-header .logo {
  display: flex;
  align-items: center;
}
.el-header .logo .wrap {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  padding: 5px;
  border: 1px solid #eee;
  box-shadow: 0 0 10px #ddd;
  background-color: #fff;
  margin-left:15px;
}

.el-header .logo .wrap img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
}

.toggle-button {
  background-color: #373D41;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>